﻿using MiniExcelLibs;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 完工订单定时任务.http;

namespace 完工订单定时任务.报表
{
    public partial class Form拉伸成品率 : Form
    {
        public Form拉伸成品率()
        {
            InitializeComponent();
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button查询_Click(object sender, EventArgs e)
        {
            string url = "http://115.231.66.30:23001/ViewReport/拉伸成品率";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("单据日期开始", dateTimePicker单据日期开始.Value.ToString("yyyy-MM-dd"));
            dic.Add("单据日期结束", dateTimePicker单据日期结束.Value.AddDays(1).ToString("yyyy-MM-dd"));
            dic.Add("工单号", textBox工单号.Text);

            DataTable dt = HttpHelper.HttpPost<DataTable>(url, JsonConvert.SerializeObject(dic));
            dataGridView1.DataSource = dt;
            dataGridView1.Refresh();

            //查询();
        }

        private void 查询()
        {
            string sql = @"SELECT [订单日期]
                          ,[工单号]
                          ,[工单状态]
                          ,[产品型号]
                          ,[产线]
                          ,[投入原料实时合计数量]
                          ,[MES母卷入库数量]
                          ,[副产品合计]
                          ,[拉伸成品率]
                          ,[总工时]
                          ,[工单开始时间]
                          ,[工单结束时间]
                          ,[小时产量]
                      FROM [VIEW_拉伸报表] where 1=1";

            if (dateTimePicker单据日期开始.Value != null)
            {
                sql += $"and [订单日期] >= '{dateTimePicker单据日期开始.Value.ToString("yyyy-MM-dd")}'";
            }
            if (dateTimePicker单据日期结束.Value != null)
            {
                sql += $"and [订单日期] < '{dateTimePicker单据日期结束.Value.AddDays(1).ToString("yyyy-MM-dd")}'";
            }
            if (!string.IsNullOrEmpty(textBox工单号.Text))
            {
                sql += $"and [工单号] = '{textBox工单号.Text}'";
            }
            DataTable dt = SQLSERVERSqlSugarHelp.scope.Ado.GetDataTable(sql);
            dataGridView1.DataSource = dt;
            dataGridView1.Refresh();
        }

        private void button导出_Click(object sender, EventArgs e)
        {
            //导出excel
            if (dataGridView1.DataSource != null)
            {
                DataTable dt = (DataTable)dataGridView1.DataSource;
                string fileName = "拉伸成品率" + DateTime.Now.ToFileTime() + ".xlsx";
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "All files (*.*)|*.*";
                saveFileDialog.FileName = fileName;
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    MiniExcel.SaveAs(saveFileDialog.FileName, dt);
                    MessageBox.Show("导出成功");
                }

            }
            else
            {
                MessageBox.Show("没有数据");
            }
        }

        private void Form拉伸成品率_Load(object sender, EventArgs e)
        {
            dateTimePicker单据日期开始.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        }
    }
}
